import ParentChild from "@/models/parent-child";
class ParentChildren {
  private menuList: Array<ParentChild>;
  private firstId: string;

  public constructor(firstId: string, menuList: Array<ParentChild>) {
    this.menuList = menuList;
    this.firstId = firstId;
  }

  /**根据父节点获取所有子节点树形结构 */
  public getParentOfChilds(): Array<ParentChild> {
    let treeMenus = new Array<ParentChild>();
    for (let menuNode of this.getRootNode()) {
      menuNode = this.buildChildTree(menuNode);
      treeMenus.push(menuNode);
    }
    return treeMenus;
  }

  /**根据子节点获取所有父节点 */
  public getChildOfParents(): ParentChild {
    let menuList = new Array<ParentChild>();
    let isWhile = true;
    let parentId = this.firstId;
    let rootId = "";
    while (isWhile) {
      if (this.isParentNode(parentId)) {
        let menu = this.getParentNode(this.firstId);
        parentId = menu.ParentId;
        rootId = menu.Id;
        menuList.push(menu);
      } else {
        isWhile = false;
      }
    }
    let firstMenu = this.getNodeById(rootId);
    let treeMenu = this.selectChildTree(firstMenu, menuList);
    return treeMenu;
  }

  //递归，建立子树形结构
  private buildChildTree(pNode: ParentChild): ParentChild {
    let chilMenus = new Array<ParentChild>();
    for (let menuNode of this.menuList) {
      if (menuNode.ParentId == pNode.Id) {
        //有子节点调用创建子节点的方法，创建子节点的子节点
        chilMenus.push(this.buildChildTree(menuNode));
      }
    }
    if (chilMenus.length > 0) {
      pNode.IsLeaf = false;
    } else {
      pNode.IsLeaf = true;
    }
    pNode.Children = chilMenus;
    return pNode;
  }
  //递归，建立子树形结构
  private selectChildTree(pNode: ParentChild, menuList: Array<ParentChild>): ParentChild {
    let chilMenus = new Array<ParentChild>();
    for (let menuNode of menuList) {
      if (menuNode.ParentId == pNode.Id) {
        //有子节点调用创建子节点的方法，创建子节点的子节点
        chilMenus.push(this.selectChildTree(menuNode, menuList));
      }
    }
    if (chilMenus.length > 0) {
      pNode.IsLeaf = false;
    } else {
      pNode.IsLeaf = true;
    }
    pNode.Children = chilMenus;
    return pNode;
  }
  //获取根节点
  private getRootNode(): Array<ParentChild> {
    let rootMenuList = new Array<ParentChild>();
    for (let menuNode of this.menuList) {
      if (menuNode.ParentId == this.firstId) {
        rootMenuList.push(menuNode);
      }
    }
    return rootMenuList;
  }

  /**
   * 获取上级节点
   * @param parentId 上级节点id
   */
  private getParentNode(parentId: string): ParentChild {
    let parentNode: ParentChild = new ParentChild();
    for (let menuNode of this.menuList) {
      if (menuNode.Id == parentId) {
        parentNode = menuNode;
      }
    }
    return parentNode;
  }
  /**
   * 是否存在上级节点
   * @param parentId 上级节点id
   */
  private isParentNode(parentId: string): boolean {
    for (let menuNode of this.menuList) {
      if (menuNode.ParentId == parentId) {
        return true;
      }
    }
    return false;
  }
  /**
   * 获取节点
   * @param parentId 节点id
   */
  private getNodeById(id: string): ParentChild {
    let parentNode: ParentChild = new ParentChild();
    for (let menuNode of this.menuList) {
      if (menuNode.Id == id) {
        parentNode = menuNode;
      }
    }
    return parentNode;
  }
}
export default ParentChildren;
